<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin;

use app\Request;
use app\services\system\admin\SystemAdminServices;
use hdphp\services\CacheService;

/**
 * Class：后台登陆
 * 创建人：Jack
 * 创建时间：2023/11/22 10:53
 * @package app\controller\admin
 */
class Login
{
    /**
     * @var SystemAdminServices
     */
    private $services;

    /**
     * Login constructor.
     * @param SystemAdminServices $services
     */
    public function __construct(SystemAdminServices $services)
    {
        $this->services = $services;
    }

    /**
     * 注释：登录
     * 创建人：Jack
     * 创建时间：2023/7/29 14:50
     * @return void
     */
    public function login(Request $request)
    {
        [$username, $password] = $request->getMore([
            ['username', ''],
            ['password', '']
        ], true);

        if (strlen(trim($password)) < 4 || strlen(trim($password)) > 64) {
            return app('json')->fail(400762);
        }

        validate(\app\validate\admin\setting\SystemAdminValidate::class)->scene('get')->check(['username' => $username, 'password' => $password]);
        $result = $this->services->login($username, $password, 'admin');

        return app('json')->success($result);
    }

    /**
     * 注释：手机号码登录
     * 创建人：Jack
     * 创建时间：2023/7/30 9:13
     * @param Request $request
     * @return mixed
     */
    public function mobileLogin(Request $request)
    {
        [$phone, $code] = $request->postMore([
            'phone', 'code',
        ], true);
        if (!$code) {
            return app('json')->fail('请输入验证码!');
        }
        // 验证验证码
        $verifyCode = CacheService::get('code_' . $phone);
        if (!$verifyCode) return app('json')->fail('请先获取验证码!');
        $verifyCode = substr($verifyCode, 0, 6);
        if ($verifyCode != $code) {
            CacheService::delete('code_' . $phone);
            return app('json')->fail('验证码错误!');
        }

        return app('json')->success($this->services->login($phone, $code, 'admin', true));
    }

    /**
     * 注释：短信重置密码
     * 创建人：Jack
     * 创建时间：2023/7/30 9:13
     * @param Request $request
     * @return mixed
     */
    public function resetPwd(Request $request)
    {
        [$phone, $code, $new_password] = $request->postMore([
            'phone', 'code', 'new_password'
        ], true);
        if (!$phone) {
            return app('json')->fail('请输入手机号!');
        }
        if (!check_phone($phone)) {
            return app('json')->fail('请输入正确的手机号!');
        }
        if (!$code) {
            return app('json')->fail('请输入验证码!');
        }
        if (!$new_password) {
            return app('json')->fail('请输入新密码!');
        }
        // 验证验证码
        $verifyCode = CacheService::get('code_' . $phone);
        if (!$verifyCode) return app('json')->fail('请先获取验证码!');
        $verifyCode = substr($verifyCode, 0, 6);
        if ($verifyCode != $code) {
            CacheService::delete('code_' . $phone);
            return app('json')->fail('验证码错误!');
        }
        $this->services->resetPassword($phone, $new_password);
        return app('json')->success('重置成功，请重新登录!');
    }
}